Skip to content

fix(virtual-core): ignore stale connected measurements - #1246

Merged
piecyk merged 4 commits into
TanStack:mainfrom
LeoLB-Wang:codex/fix-stale-index-measurement
Jul 31, 2026
Merged

fix(virtual-core): ignore stale connected measurements#1246
piecyk merged 4 commits into
TanStack:mainfrom
LeoLB-Wang:codex/fix-stale-index-measurement

Conversation

@LeoLB-Wang

@LeoLB-Wang LeoLB-Wang commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Changes

  • Ignore connected measured nodes whose data-index falls outside the current count before any key lookup or measurement callback.
  • Add a 22-to-1 count-shrink regression that independently exercises the ref and delayed ResizeObserver ingress paths.
  • Add a patch changeset for @tanstack/virtual-core.

Why

#1148 safely handles disconnected stale nodes. A node can also remain connected while React applies a count shrink; its old data-index can then reach getItemKey, which commonly indexes application data and may throw.

Testing

  • pnpm run test:pr
  • pnpm --filter @tanstack/virtual-core test:lib -- index.test.ts

Related

Checklist

  • Followed the contributing guide.
  • Tested locally with pnpm run test:pr.
  • Added a changeset for the published package.

Summary by CodeRabbit

  • Bug Fixes

    • Virtualized lists now safely ignore measurements from stale items after the item count decreases.
    • Prevents invalid measurement updates and related errors from outdated connected elements.
    • Improves stability when items are removed while their previous elements remain connected or trigger resize events.
    • Ensures only currently valid items are measured and updated.
  • Tests

    • Added regression coverage for stale measurements and resize events after list size changes.

@coderabbitai

coderabbitai Bot commented Jul 30, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 585f50e2-d585-4f4e-b292-dfd867aa76e6

📥 Commits

Reviewing files that changed from the base of the PR and between c8c6fd2 and 3160b99.

📒 Files selected for processing (1)
  • packages/virtual-core/src/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/virtual-core/src/index.ts

📝 Walkthrough

Walkthrough

Virtualizer measurement paths now ignore indexes outside the current item count. A regression test covers stale refs and delayed ResizeObserver callbacks after reducing count. A patch changeset documents the behavior.

Changes

Stale measurement handling

Layer / File(s) Summary
Guard measurement indexes
packages/virtual-core/src/index.ts, .changeset/soft-tigers-sleep.md
A shared range check makes ResizeObserver, direct measurement, and item resizing return early for invalid indexes. The changeset records a patch release.
Validate stale node handling
packages/virtual-core/tests/index.test.ts
Regression coverage verifies that stale refs and late ResizeObserver callbacks do not access out-of-range item keys or throw after count shrinks.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the fix to ignore stale connected measurements in virtual-core.
Description check ✅ Passed The description explains the change, motivation, tests, related issues, checklist, and changeset impact, but omits the explicit Release Impact section.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Comment thread packages/virtual-core/src/index.ts Outdated
Co-authored-by: Damian Pieczynski <piecyk@gmail.com>

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/virtual-core/src/index.ts`:
- Around line 492-495: Update the conditional in the surrounding index-handling
method to wrap the isIndexInRange(index) expression in parentheses, making the
if statement valid TypeScript while preserving its existing early-return
behavior.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 81b0b608-79f1-4e25-a759-8d899a35c5e8

📥 Commits

Reviewing files that changed from the base of the PR and between 79f2ab7 and 84175bb.

📒 Files selected for processing (1)
  • packages/virtual-core/src/index.ts

Comment thread packages/virtual-core/src/index.ts Outdated
@nx-cloud

nx-cloud Bot commented Jul 31, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 3160b99

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 4m 2s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 20s View ↗

☁️ Nx Cloud last updated this comment at 2026-07-31 03:38:26 UTC

@pkg-pr-new

pkg-pr-new Bot commented Jul 31, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-virtual

npm i https://pkg.pr.new/@tanstack/angular-virtual@1246

@tanstack/lit-virtual

npm i https://pkg.pr.new/@tanstack/lit-virtual@1246

@tanstack/marko-virtual

npm i https://pkg.pr.new/@tanstack/marko-virtual@1246

@tanstack/react-virtual

npm i https://pkg.pr.new/@tanstack/react-virtual@1246

@tanstack/solid-virtual

npm i https://pkg.pr.new/@tanstack/solid-virtual@1246

@tanstack/svelte-virtual

npm i https://pkg.pr.new/@tanstack/svelte-virtual@1246

@tanstack/virtual-core

npm i https://pkg.pr.new/@tanstack/virtual-core@1246

@tanstack/vue-virtual

npm i https://pkg.pr.new/@tanstack/vue-virtual@1246

commit: 3160b99

@piecyk
piecyk merged commit d2cf98b into TanStack:main Jul 31, 2026
10 checks passed
@piecyk

piecyk commented Jul 31, 2026

Copy link
Copy Markdown
Collaborator

Thanks @LeoLB-Wang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants